package code.structure.manager.bitmap
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	
	/**
	 *显示对象转换器 
	 * @author Yuri
	 * 
	 */
	public class YDisplayConvert
	{
		public function YDisplayConvert()
		{
		}
		
		/**
		 *转换 
		 * @param subKey
		 * @param sourceClass
		 * @param completeDecodeAllComplete
		 * 
		 */
		public static function convert(subKey:String,sourceClass:Class,completeDecodeAllComplete:Function):void{
			var dis:DisplayObject = new sourceClass as DisplayObject;
			var arr:Array;
			if(dis as MovieClip){
				arr = drawMoveClip(dis as MovieClip);
			}else if(dis as BitmapData){
				arr = [0,0,dis];
			}else{
				return;
			}
			completeDecodeAllComplete(subKey,arr);
			dis = null;
		}
		
		/** 
		 * 绘制动画 
		 * @param dis
		 * @return 
		 * 
		 */
		public static  function drawMoveClip(dis:MovieClip,selfAdd:int=1):Array{
			var totleCount:int = dis.totalFrames;
			
			var count:int=0;
			var arr:Array = [];
			
			while(count<totleCount){
				count +=selfAdd;
				dis.gotoAndStop(count);
				
				var frameArr:Array = [];
				//检测是否只有一个子对象并且还是位图信息直接获取不需要draw
				if(dis.numChildren == 1){
					var childM:* = dis.getChildAt(0);
					if(childM as Bitmap){
						frameArr[0] = Math.round(childM.x);
						frameArr[1] = Math.round(childM.y);
						frameArr[2] = (childM as Bitmap).bitmapData;
						arr.push(frameArr);
						continue;
					}
				}
				makeAllChildrenGoToFrame(dis,count);
				var rectangle:Rectangle = dis.getRect(dis);
				var bWidth:int = Math.round((dis.width<1)?1:dis.width);
				var bHeight:int =Math.round((dis.height<1)?1:dis.height);
				var bitmap:BitmapData = new BitmapData(bWidth,bHeight,true, 0x00000000);
				bitmap.draw(dis,new Matrix(dis.scaleX,0,0,dis.scaleY,-rectangle.x,-rectangle.y), null, null, null, true);
				frameArr[0] = Math.round(rectangle.x);
				frameArr[1] = Math.round(rectangle.y);
				frameArr[2] = bitmap;
				arr.push(frameArr);
			}
			return arr;
		}
		
		private static function makeAllChildrenGoToFrame(m:*, f:int):void
		{
			if(m is DisplayObjectContainer)
			{
				for (var i:int = 0; i < m.numChildren; i++) {
					var c:* = m.getChildAt(i);
					makeAllChildrenGoToFrame(c, f);
					if (c is MovieClip) {
						c.gotoAndStop(f);
					}
				}
			}
		}
		
		/**
		 *画bitmapdata 
		 * @param mc
		 * @return 
		 * 
		 */
		public static function  drawBitMapData(mc:DisplayObject):Array{
			var arr:Array = [];
			var rectangle:Rectangle = mc.getRect(mc);
			var bmd:BitmapData = new BitmapData(mc.width, mc.height, true, 0);
			bmd.draw(mc,new Matrix(mc.scaleX,0,0,mc.scaleY,-rectangle.left,-rectangle.top), null, null, null,true);
			arr.push(bmd,rectangle.left,rectangle.top);
			return arr;
		}
		
		/**
		 *通过类来获取位图数据的所有信息 
		 * @param c
		 * @return 
		 * 
		 */
		public static function drawBitMapDataInfoByClass(c:Class):Array{
			if(!c) return null;
			var d:* = new c();
			if(!d) return null;
			if(d as BitmapData) return [d,0,0];
			else if(d as DisplayObject){
				return( drawBitMapData(d));
			}
			return null;
		}
		
		/**
		 *通过 类 draw出位图数据 
		 * @param c
		 * @return 
		 * 
		 */
		public static function drawBitMapDataByClass(c:Class):BitmapData{
			var arr:Array = drawBitMapDataInfoByClass(c);
			if(!arr) return null;
			return arr[0];
		}
	}
}